import java.util.Vector;

public class Permutation {
	public static void main(String[] args) {
		Permutation p = new Permutation();
		String s = "12345";
		Vector<String> v = p.permute(s.toCharArray());
		int c = 1;
		for (String sp : v) {
			System.out.println((c++) + ". " + sp);
		}
	}

	public Vector<String> permute(char[] arr) {
		return permute(arr, 0, arr.length);
	}

	private Vector<String> permute(char[] arr, int h, int t) {
		if (h == t - 1) {
			// System.out.println(String.valueOf(arr));
			Vector<String> v = new Vector<String>(1);
			v.add(String.valueOf(arr));
			return v;
		}

		else {
			Vector<String> v = new Vector<String>((int) factorial(t - h));
			for (int i = h; i < t; ++i) {
				swap(arr, i, h);
				Vector<String> subv = permute(arr, h + 1, t);
				swap(arr, i, h);
				v.addAll(subv);
			}
			return v;
		}
	}

	private void swap(char[] arr, int i, int j) {
		if (i != j) {
			arr[i] ^= arr[j];
			arr[j] ^= arr[i];
			arr[i] ^= arr[j];
		}
	}

	private long factorial(int n) {
		long f = 1;
		for (; n != 0; --n) {
			f *= (long) n;
		}
		return f;
	}
}
